<template>
  <div class="app-container">
    <el-form :model="form" :rules="rules" label-width="120px" ref="HelpForm">
      <el-form-item label="菜单名" prop="name" required>
        <el-input v-model="form.name" style="width:202px" />
      </el-form-item>

      <el-form-item label="链接" prop="link">
        <el-input v-model="form.link" style="width:300px" />
      </el-form-item>

      <el-form-item label="图标" prop="icon" required="">
        <img @click="showImageCenter" v-if="form.icon" :src="form.icon" class="avatar">
        <i v-else @click="showImageCenter" class="el-icon-plus avatar-uploader-icon"></i>
      </el-form-item>

      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" type="number" style="width:202px" />
      </el-form-item>

      <el-form-item label="状态" prop="status" required>
        <el-switch v-model="form.status" active-text="上线" inactive-text="下架">
        </el-switch>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit('HelpForm')" size="small">提 交</el-button>
        <el-button @click="onCancel()" size="small">重置</el-button>
      </el-form-item>
    </el-form>
    <ImageCenter :is_multiple=false :Innervisible.sync="dialogImageVisible" :limit=1 @change="changeImage"
      @closeImage="closeImage" />
  </div>
</template>

<script>
import { createIndexMenu, updateIndexMenu, detailIndexMenu } from '@/api/config/indexMenu'
import ImageCenter from '@/components/ImageCenter'
export default {
  components: { ImageCenter },
  data() {
    return {
      form: {
        name: '',
        link: '',
        icon: '',
        sort: 99,
        status: true,
      },
      dialogImageVisible: false,
      rules: {
        name: [{ required: true, message: 'name不能为空', trigger: 'blur' }],
      }
    }
  },
  beforeMount() {
    let id = this.$route.params.id || 0;
    if (id > 0) {
      detailIndexMenu(id).then(response => {
        this.form = response.data
        this.form.status = response.data.status ? true : false
      })
    }
  },
  methods: {
    showImageCenter() {
      this.dialogImageVisible = true;
      return false;
    },
    changeImage: function (res) {
      this.dialogImageVisible = false;
      if (res.length == 1) {
        this.form.icon = res[0].url
      }
    },
    closeImage: function () {
      this.dialogImageVisible = false;
    },
    onSubmit(formName) {

      this.form.status = this.form.status ? 1 : 0

      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.checkSpec()
          let id = this.$route.params.id || 0;
          if (id > 0) {
            updateIndexMenu(this.form).then(response => {
              if (response.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                setTimeout(() => {
                  this.$router.push({ name: 'indexMenu' });
                }, 200)
              }
            })
          } else {
            createIndexMenu(this.form).then(response => {
              if (response.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                setTimeout(() => {
                  this.$router.push({ name: 'indexMenu' });
                }, 200)
              }
            })
          }
        }
      })
    },

    checkSpec() {
      let _spec_name = this.form.name
      if (!_spec_name || _spec_name.length < 1) {
        this.$message({
          message: '标题不能为空',
          type: 'warning'
        })
        return false;
      }

      let _content = this.form.link
      if (!_content || _content.length < 1) {
        this.$message({
          message: '链接不能为空',
          type: 'warning'
        })
        return false;
      }

      let icon = this.form.icon
      if (!icon || icon.length < 1) {
        this.$message({
          message: '图标不能为空',
          type: 'warning'
        })
        return false;
      }

      return true;
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}

.avatar,.avatar-uploader-icon {
  width: 90px;
  height: 90px;
  padding: 1px;
  border: 1px solid #ccc;
  text-align: center;
  line-height: 90px;
}

.app-container {
  background: #fff;
  margin-top: 18px;
  height: 100%;
  min-height: calc(100vh - 68px);
  font-size: 12px
}

.app-container * {
  font-size: 12px;
}
</style>
